package com.zdkj.subway.stm.config;

import com.zdkj.common.data.tenant.TenantContextHolder;
import com.zdkj.common.sequence.builder.DbSeqBuilder;
import com.zdkj.common.sequence.properties.SequenceDbProperties;
import com.zdkj.common.sequence.sequence.Sequence;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

/**
 * @author chen
 * @date 2020年1月16日10:28:54
 * <p>
 * 设置发号器生成规则
 */
@Component
@Configuration
public class SequenceConfig {



	/**
	 * 通道编号发号器
	 *
	 * @param dataSource
	 * @param properties
	 * @return
	 */
	@Bean
	public Sequence channelSequence(DataSource dataSource,
									SequenceDbProperties properties) {
		return DbSeqBuilder
				.create()
				.bizName(() -> String.format("channel_%s", TenantContextHolder.getTenantId()))
				.dataSource(dataSource)
				.step(properties.getStep())
				.retryTimes(properties.getRetryTimes())
				.tableName(properties.getTableName())
				.build();
	}
}
